class Solution {
public:
    int minOperations(vector<int>& nums, int x) {
        int left = 0, right = 0, sum = 0;
        int n = nums.size();

        int Arrsum = 0;

        for (auto e : nums)
            Arrsum += e;

        int target = Arrsum - x;
        if (target < 0)
            return -1;

        int len = -1;
        while (right < n)
        {
            sum += nums[right];
            while (sum > target)
            {
                sum -= nums[left];
                left++;
            }

            if (sum == target)
                len = max(len, right - left + 1);

            right++;
        }
        return len == -1 ? len : n - len;
    }
};